/*
TO DO:

*/

#include <iostream>
#include <cstdlib>
#include "LibraryMember.h"

using namespace std;

int LibraryMember::NextID = 1;


void LibraryMember::MemberDetails()
{
    cout << "Member details: " << endl;
    cout << "Member type: " << TypeName << endl;
    cout << "Name: " << Name << endl;
    cout << "Address: "<< Address << endl;
    cout << "Availability: "<< Availability << endl;
    cout << "Loans: ";
    if( LoansOfMember.size()==0 ) // ha nincs kolcsonzes
    {
        cout << "No loanings" << endl;
    }
    else // felsoroljuk a kolcsonzeseket
    {
        cout << endl;
        for(unsigned int i=0; i<LoansOfMember.size(); i++)
        {
            cout << i+1 << ".\t" << LoansOfMember[i]->GetBookID() << endl; // adott konyv kiirasa
        }
    }
}


bool LibraryMember::Loan_LOAD(Book* mitmit)
{
    if( mitmit->Loan(this) )
    {
        LoansOfMember.push_back(mitmit);
        return true;
    }
    return false;
}


bool LibraryMember::Loan(Book* mitmit)
{
    if( LoansOfMember.size() >= Maximum )
    {
        return false;
    }
    else
    {
        return Loan_LOAD(mitmit);
    }
}


int LibraryMember::Return(const string& BookID)
{
    int Difference = 0; // ez lenne a keses
    bool Found = false;
    vector<Book*>::iterator it;
    for( it=LoansOfMember.begin(); it != LoansOfMember.end(); ++it )
    {
        if( (*it)->GetBookID() == BookID )
        {
            Found = true;
            break;
        }
    }
    if( Found )
    {
        Difference = 0; // ide nem tudtam mit irni, ezert lett kinullazva...
        (*it)->Return();
        LoansOfMember.erase(it);
        return Difference;
    }
    else
    {
        throw INVALID_RETURN;
    }
}


void LibraryMember::ModifyMember(int Funct, std::string& NewData)
{
    switch(Funct)
    {
        case 1:
            Name = NewData;
            break;
        case 2:
            Address = NewData;
            break;
        case 3:
            Availability = NewData;
            break;
    }
}


void LibraryMember::ListMember()
{
    cout << "MemberID: " << GetLibraryMemberID() << endl;
    cout << "Name: " << GetName() << endl;
    cout << "Address: " << GetAddress() << endl;
    cout << "Availability: " << GetAvailability() << endl;
    cout << "Type: " << GetTypeName() << endl;
    cout << "Number of loans: " << LoansOfMember.size() << endl;
}


ostream& operator<<(ostream &os,const LibraryMember *libmem) ///////////////////////////////////////////////////////////
{
    os << libmem->GetTypeID() << ';' << libmem->GetTypeName() << ';' <<  libmem->GetName()<< ';' << libmem->GetLibraryMemberID() << ';' << libmem->GetAddress() << ';' << libmem->GetAvailability() << ';' << libmem->LoansOfMember.size(); //;-el elválasztva az alap adatok, és hogy hány kölcsönzés van
    if(libmem->LoansOfMember.size()!=0)
    {
        os <<';';
        for(unsigned int i=0; i<libmem->LoansOfMember.size(); i++)
        {
            //os << libmem->LoansOfMember[i]->GetBookID() << ';' << libmem->LoansOfMember[i]->GetDeadline(); //kiírjuk a könyv id-ját, és a kezdeti dátumot
            os << libmem->LoansOfMember[i]->GetBookID();
            if( i+1<libmem->LoansOfMember.size() ) { os <<';'; }
        }
    }
    return os;
}

